package com.ruoyi.web.controller.common;

import com.aliyun.oss.OSS;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.oss.OSSClientUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: yan
 * @Description: 文件上传
 * @Date:
 */
@Api(tags = {"h5/文件上传"})
@RestController
@RequestMapping("/common/oss")
public class OSSController {

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) {
        OSSClientUtil ossClientUtil = new OSSClientUtil();
        OSS ossClient = ossClientUtil.getOssClient();
        try {
            String oss = ossClientUtil.uploadImg2Oss(file, ossClient);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", "https://aitax.oss-cn-beijing.aliyuncs.com/ai/" + oss);
            ajax.put("fileName", file.getName());
            ajax.put("newFileName",oss);
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
}
